package com.yyryyty.chapter10;

import java.util.Objects;
import java.util.Optional;

/**
 * @Description :
 * @Author : YTY
 * @Date : 2021-01-20 16:07
 */
public class OptionalInit {

    public static void main(String[] args) {

//        Optional<Object> optional = Optional.of(null);

//        Insurance insurance = new Insurance();
//        Optional.ofNullable(insurance)
//                .map(Insurance::getName)
//                .ifPresent(System.out::println);

        Optional<Insurance> insuranceOptional = Optional.ofNullable(new Insurance());
        insuranceOptional
                .filter(insurance -> !Objects.equals("WAF-YTY", insurance.getName()))
                .ifPresent(insurance -> System.out.println("OK"));

    }

    public String getCarInsuranceName(Optional<Person> person) {

        return person.flatMap(Person::getCar)
                .flatMap(Car::getInsurance)
                .map(Insurance::getName)
                .orElse("Unknown");
    }
}
